package saucaca.Adapter;

import java.util.ArrayList;
import java.util.List;

import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;
import saucaca.Object.SingerObject;
import saucaca.Util.ImageDisplayListener;
import saucaca.Util.TagName;
import saucaca.fragment.ArtistDetailFragment;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ArtistAdapter extends BaseAdapter {
	Context context;
	private String tag;
	private LayoutInflater inflater;
	private List<SingerObject> list;
	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	private ImageDisplayListener imageListener;

	public ArtistAdapter(Context context,List<SingerObject> _list) {
		list = _list;
		this.context = context;
		this.inflater = LayoutInflater.from(context);

		options = new DisplayImageOptions.Builder().showImageOnFail(R.drawable.ic_default).showStubImage(R.drawable.ic_default).showImageForEmptyUri(R.drawable.ic_default).cacheInMemory().cacheOnDisc().build();
		imageListener = new ImageDisplayListener();
	}

	public String getTag() {
		return tag;
	}

	public void setList(ArrayList<SingerObject> _list, String _tag) {
		list = _list;
		this.tag = _tag;
		notifyDataSetChanged();
	}

	public List<SingerObject> getList() {
		return list;
	}

	class ViewHolder {
		RelativeLayout layout;
		ImageView imgeAvatar;
		TextView textViewName;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_artist_listartist, null);
			holder.layout = (RelativeLayout) convertView.findViewById(R.id.artist_item_layout);
			holder.layout.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					SingerObject singerObject=list.get(position);
					((MainActivity)context).switchContent(ArtistDetailFragment.class.getName(), singerObject);
				}
			});
			holder.imgeAvatar = (ImageView) convertView.findViewById(R.id.artist_picture);
			holder.textViewName = (TextView) convertView.findViewById(R.id.artist_txtName);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.textViewName.setText(list.get(position).getTXNAME());
		imageLoader.displayImage(list.get(position).getAVATAR(), holder.imgeAvatar, options, imageListener);
		return convertView;
	}
	
	private String SendDataSinger(SingerObject singerObject) {
		return singerObject.getSINGER_ID()+TagName.keyBundle+singerObject.getSINGER_CODE()+TagName.keyBundle+singerObject.getTXNAME()+TagName.keyBundle+singerObject.getAVATAR()+TagName.keyBundle+singerObject.getRN();
	}

}
